package offer;

public class offerII_0105 {
    public int maxAreaOfIsland(int[][] grid) {
        int m= grid.length;
        int n=grid[0].length;
        boolean[][] used=new boolean[m][n];
        int max=0;
        for(int i=0;i<m;++i){
            for(int j=0;j<n;++j){
                if(!used[i][j]&&grid[i][j]==1){
                    max=Math.max(max,search(grid,used,i,j,m,n));
                }
                else used[i][j]=true;
            }
        }
        return max;
    }
    public int search(int[][] grid,boolean[][] used,int i,int j,int m,int n){
        if(i>=0&&i<m&&j>=0&&j<n&&used[i][j]==false){
            used[i][j]=true;
            if(grid[i][j]==1) return 1+search(grid, used, i+1, j, m, n)+search(grid, used, i, j+1, m, n)
                    +search(grid, used, i-1, j, m, n)+search(grid, used, i, j-1, m, n);

        }
        return 0;
    }
}
